package kr.saeildamil.androidstudy.ch16;

import java.util.Calendar;

import kr.saeildamil.androidstudy.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class AlertDialogTest extends Activity {
	final static int SAMPLEDLG = 0;
	final static int QUESTiONDLG = 1;
	int a = 3;
	int b = 4;
	int result;
	int mSelect = 0;
	boolean[] mSelect2 = { false, false, false, false };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch16_alertdialogtest);
	}

	public void mOnClick(View v) {
		switch (v.getId()) {
		case R.id.ch16_alertdia_btn01:
			AlertDialog.Builder bld01 = new AlertDialog.Builder(this);
			bld01.setTitle("알립니다");
			bld01.setMessage("대화상자를 열었습니다.");
			bld01.setIcon(R.drawable.kimth);
			bld01.show();
			break;

		case R.id.ch16_alertdia_btn02:
			new AlertDialog.Builder(this).setTitle("김태희가 고백을함")
					.setMessage("고백을 받아야만 닫힙니다.").setIcon(R.drawable.kimth)
					.setNegativeButton("받기", null).setCancelable(false).show();
			break;

		case R.id.ch16_alertdia_btn03:
			new AlertDialog.Builder(this).setTitle("알립니다")
					.setMessage("대화상자를 열었습니다.").setIcon(R.drawable.kimth)
					.setNegativeButton("Negative", null)
					.setPositiveButton("Positive", null)
					.setNeutralButton("Neutral", null).show();
			break;

		case R.id.ch16_alertdia_btn04:
			showDialog(SAMPLEDLG);
			break;

		case R.id.ch16_alertdia_btn05:
			showDialog(QUESTiONDLG);
			break;

		case R.id.ch16_alertdia_btn06:
			MakeError();
			break;

		case R.id.ch16_alertdia_btn07:
			new AlertDialog.Builder(this).setTitle("질문")
					.setMessage("3과 4에 대하여 어떤 연산을 하시겠습니까?")
					.setPositiveButton("덧셈", listener)
					.setNegativeButton("곱셈", listener).show();
			break;

		case R.id.ch16_alertdia_btn08:
			new AlertDialog.Builder(this)
					.setTitle("원하시는 연예인을 선택하세요")
					.setIcon(R.drawable.iu)
					.setSingleChoiceItems(R.array.girls, mSelect,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									mSelect = which;
								}
							})
					.setPositiveButton("확인",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									String[] girls = getResources()
											.getStringArray(R.array.girls);
									TextView tv = (TextView) findViewById(R.id.ch16_alertdia_tv02);
									tv.setText("선택한 연예인은  " + girls[mSelect]
											+ "입니다.");
								}
							}).setNegativeButton("취소", null).show();
			break;
		case R.id.ch16_alertdia_btn09:
			new AlertDialog.Builder(this)
					.setTitle("연예인을 선택하세요(다수 가능)")
					.setIcon(R.drawable.iu)
					.setMultiChoiceItems(R.array.girls, mSelect2, listener2)
					.setPositiveButton("확인",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton2) {
									String[] girls = getResources()
											.getStringArray(R.array.girls);
									TextView tv = (TextView) findViewById(R.id.ch16_alertdia_tv03);
									String string = "선택한 연예인은";
									for (int i = 0; i < mSelect2.length; i++) {
										if (mSelect2[i]) {
											string += girls[i] + " ";
										}
									}
									tv.setText(string);
								}
							})
					.setNegativeButton("취소", null).show();

			break;

		}
	} // end of method mOnClick

	DialogInterface.OnMultiChoiceClickListener listener2 = new OnMultiChoiceClickListener() {
		public void onClick(DialogInterface dialog, int which, boolean isChecked) {
			mSelect2[which] = isChecked;
		}
	};

	DialogInterface.OnClickListener listener = new OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			if (which == DialogInterface.BUTTON_POSITIVE) {
				result = a + b;
			} else {
				result = a * b;
			}

			TextView tv = (TextView) findViewById(R.id.ch16_alertdia_tv);
			tv.setText("연산결과는  " + result);
			Toast.makeText(AlertDialogTest.this, "연산완료", Toast.LENGTH_LONG)
					.show();
		}
	};

	private void MakeError() {
		try {
			Thread.sleep(3000);
		} catch (Exception e) {
			e.getStackTrace();
		}

		boolean stat = true; // 예외 발생
		if (stat) {
			new AlertDialog.Builder(this).setTitle("예외발생!!!")
					.setMessage("어처구니없는 이유로 에러 발생 ㅡㅡ ")
					.setPositiveButton("알았다", new OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							finish();// 액티비티 종료
						}
					}).show();

		}
		Toast.makeText(this, "정상완료!", Toast.LENGTH_SHORT).show();
	}

	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		if (id == SAMPLEDLG) {
			return new AlertDialog.Builder(this).setTitle("알림 대화상자")
					.setMessage("대화상자 오픈").setIcon(R.drawable.kimth)
					.setPositiveButton("닫기", null).setCancelable(false)
					.create();
		} else {
			return new AlertDialog.Builder(this).setTitle("질문 대화상자")
					.setMessage("밥먹었나요?").setPositiveButton("먹었음", null)
					.setNegativeButton("굶었음", null).create();
		}
	}

	@Override
	@Deprecated
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
		switch (id) {
		case SAMPLEDLG:
			break;
		case QUESTiONDLG:
			Calendar cal = Calendar.getInstance();
			String sTime = String.format("%d시 %d분 %d초", cal.get(Calendar.HOUR),
					cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));

			dialog.setTitle(sTime);
			break;
		}
	}
}
